const { Controller } = require('egg')
const fs = require('fs')
const { rand } = require('../utils/rand');
const qiniu = require('qiniu');
const moment = require('moment');


class OneisallController extends Controller {
    static TABLE_ONE = 'one_is_all'

    async index() {
        let ymd = this.ctx.query.ymd
        if (!ymd) {
            ymd = moment(new Date()).format('YYYY-MM-DD')
        }
        const data = await this.app.mysql.get(OneisallController.TABLE_ONE, {
            ymd
        })
        const ymdList = await this.app.mysql.select(OneisallController.TABLE_ONE, {
            columns: ['id', 'ymd', 'vol', 'article_title', 'article_author', 'question_title'],
            orders: [['id', 'desc']]
        })
        
        const asyncToday = ymdList.map(v => v.ymd).includes(moment(new Date()).format('YYYY-MM-DD'))
        const ymdListJson = JSON.stringify(ymdList)
        if (!data) {
            await this.ctx.render('oneisall/404.html', { ymdListJson, asyncToday })
        } else {
            await this.ctx.render('oneisall/index.html', {
                ymdListJson,
                asyncToday,
                ymd: data.ymd,
                image: data.image,
                text: data.text,
                vol: data.vol,
                article_title: data.article_title,
                article_author: data.article_author,
                article_content: data.article_content,
                question_title: data.question_title,
                question_content: data.question_content
            })
        }
    }
    
    async runCrontabTaskManual() {
        const res = await this.app.runSchedule('syncOne')
        this.ctx.body = { ...res }
    }
}

module.exports = OneisallController
